<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递公司名称" prop="expressCompany">
        <el-input
          v-model="queryParams.expressCompany"
          placeholder="请输入快递公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计费方式" prop="calculationMethod">
        <el-input
          v-model="queryParams.calculationMethod"
          placeholder="请输入计费方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否默认模板" prop="isDefault">
        <el-select v-model="queryParams.isDefault" placeholder="请选择是否默认" clearable>
          <el-option
            v-for="dict in dict.type.ec_currect_and_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.ec_currect_and_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:template:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:template:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:template:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:template:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板ID" align="center" prop="templateId" />
      <el-table-column label="模板名称" align="center" prop="templateName" />
      <el-table-column label="快递公司名称" align="center" prop="expressCompany" />
      <el-table-column label="快递公司编码" align="center" prop="expressCode" />
      <el-table-column label="计费方式" align="center" prop="calculationMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ec_algo_payment_type" :value="scope.row.calculationMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="首重/首件" align="center" prop="firstWeight" />
      <el-table-column label="首重/首件费用" align="center" prop="firstFee" />
      <el-table-column label="续重/续件" align="center" prop="additionalWeight" />
      <el-table-column label="续重/续件费用" align="center" prop="additionalFee" />
      <el-table-column label="包装费" align="center" prop="packagingFee" />
      <el-table-column label="免邮金额" align="center" prop="freeShippingAmount" />
      <el-table-column label="配送区域" align="center" prop="deliveryAreas" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.deliveryAreas">
            <el-tag
              v-for="(area, index) in parseDeliveryAreas(scope.row.deliveryAreas)"
              :key="index"
              type="primary"
              plain
              size="mini"
              style="margin: 2px;"
            >
              {{ area }}
            </el-tag>
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="是否默认模板" align="center" prop="isDefault">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ec_currect_and_no" :value="scope.row.isDefault"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ec_currect_and_no" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:template:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:template:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listTemplate, delTemplate } from "@/api/system/template"

export default {
  name: "Template",
  dicts: ['ec_currect_and_no', 'ec_algo_payment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运费模板表格数据
      templateList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateName: null,
        expressCompany: null,
        expressCode: null,
        calculationMethod: null,
        firstWeight: null,
        firstFee: null,
        additionalWeight: null,
        additionalFee: null,
        packagingFee: null,
        freeShippingAmount: null,
        deliveryAreas: null,
        isDefault: null,
        status: null,
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 解析配送区域JSON字符串为数组 */
    parseDeliveryAreas(deliveryAreas) {
      if (!deliveryAreas) return []
      try {
        const areas = JSON.parse(deliveryAreas)
        return Array.isArray(areas) ? areas : []
      } catch (e) {
        return deliveryAreas.split(',').filter(area => area.trim())
      }
    },
    /** 查询运费模板列表 */
    getList() {
      this.loading = true
      listTemplate(this.queryParams).then(response => {
        this.templateList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.templateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      const obj = { path: "/system/template/form", query: { pageType: "add" } }
      this.$tab.openPage("新增运费模板", obj.path, obj.query)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const templateId = row.templateId || this.ids
      const obj = { path: "/system/template/form", query: { pageType: "edit", templateId: templateId } }
      this.$tab.openPage("修改运费模板", obj.path, obj.query)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const templateIds = row.templateId || this.ids
      this.$modal.confirm('是否确认删除运费模板编号为"' + templateIds + '"的数据项？').then(function() {
        return delTemplate(templateIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/template/export', {
        ...this.queryParams
      }, `template_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
